#pragma once
#if 0
#include "main.h"
#include "game_entity.h"

struct HashGridElement
{
	V2 _pos;
	Entity * e;
	HashGridElement * next;
};

GLOBAL_CONSTANT int HASH_GRID_DATA_COUNT = (2047);
GLOBAL_CONSTANT int HASH_GRID_RESOLUTION =(7);
struct HashGrid
{
	HashGridElement data[HASH_GRID_DATA_COUNT];
};


int _hash_grid_get_key(V2 _pos)
{
	int x = (int)_pos.x - (((int)_pos.x)%HASH_GRID_RESOLUTION);
	int y = (int)_pos.y - (((int)_pos.y)%HASH_GRID_RESOLUTION);


	int Result = (x^(y<<4))%HASH_GRID_DATA_COUNT;
	//printf("key = %f -> %f\n",(float)(x^(y<<4)),(float)Result);
	return Result;
}

void hash_grid_add(HashGrid * hg, Entity * e)
{
	//assert(hg); assert(e);

	int index = -1;// _hash_grid_get_key(de->_pos);
	if (e)
	{
		index = _hash_grid_get_key(e->_pos_);
	}
//	bool check = true;

	HashGridElement * entry =0;
	if (hg)
	{
		entry = &hg->data[index];
	}
	
	if(entry)
	{
		while (entry->e)
		{
			if (entry)
			{
				if (entry->next)
				{
					entry = entry->next;
				}
				else
				{
					entry->next = new HashGridElement();
					entry->next->e = 0;
					entry->next->next = 0;
					entry->next->_pos = v2(-1,-1);
					entry = entry->next;
				}
			}
		}
	}

	if (entry && e)
	{
		entry->e = e;
		entry->next = 0;
		entry->_pos.x = e->_pos_.x;
		entry->_pos.y = e->_pos_.y;
	}
}


HashGridElement * hash_get_element_at_point(HashGrid * hg, V2 pos, HashGridElement * out_parent, int * out_key)
{
	//HashGridElementResult * Result = 0;
	HashGridElement * prev = 0;
	int key = _hash_grid_get_key(pos);
	HashGridElement * cur = &hg->data[key];

	while(cur)
	{
		if (cur->e)
		{
			if (cur->e->_pos_ == pos)
			{
				
				break;
				//return Result;
			}
		}
		prev = cur;
		cur = cur->next;
	}
	if(out_key && cur && prev)
	{
		out_parent = prev;


		*out_key = key;
		return cur;
	}
	return 0;
	//return Result;
}

std::vector<Entity*> hash_get_all_entitys_at_pos(HashGrid * hg, V2 pos)
{
	std::vector<Entity * > Result;
	HashGridElement * cur = &hg->data[_hash_grid_get_key(pos)];
	while(cur)
	{
		if (cur->e)
		{
				Result.push_back(cur->e);
		}
		cur = cur->next;
	}
	return Result;
}

std::vector<Entity *> hash_get_near_entitys(HashGrid * hg, V2 pos)
{
	return hash_get_all_entitys_at_pos(hg,pos);
}

std::vector<Entity *> hash_get_all_entitys(HashGrid * hg)
{
	std::vector<Entity * > Result;
	HashGridElement * cur = 0;
	for (int i = 0; i < ARRAY_ELEMENTS(hg->data); i++)
	{
		cur = &hg->data[i];
		while(cur)
		{
			if (cur->e){Result.push_back(cur->e);}
			cur = cur->next;
		}
	}
	return Result;
}

void hash_grid_remove_entity(HashGrid * hg, V2 pos)
{
	HashGridElement * parentE = 0;	
	int key = 0;
	HashGridElement * currentE = hash_get_element_at_point(hg,pos,parentE,&key);
	if (currentE)
	{
		bool parent = parentE && parentE->e;
		bool child = currentE && currentE->next;

		//head node
		if(!parent)
		{
			//single node
			if (!child)
			{
				hg->data[key].e = 0;
				hg->data[key].next = 0;
				hg->data[key]._pos = v2(-1.0f,-1.0f);
			}
			else	//head node
			{
				hg->data[key].e = currentE->next->e;
				hg->data[key].next = currentE->next->next;
				hg->data[key]._pos = currentE->next->_pos;
			}
		}
		else //in chain
		{
			//in middle
			if (child)
			{
				parentE->next = currentE->next;
			}
			else	//at end
			{
				parentE->next = 0;
			}
		}
	}

}

#endif